﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Globalization;

namespace WpfBinding
{
    public class NumberValidationRule : ValidationRule
    {
        private decimal _min;
        private decimal _max;

        public NumberValidationRule()
        {

        }

        public decimal Min
        {
            get { return _min; }
            set { _min = value; }
        }

        public decimal Max
        {
            get { return _max; }
            set { _max = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            decimal age = 0;

            try
            {
                if (((string)value).Length > 0)
                    age = decimal.Parse((String)value);
            }
            catch (Exception e)
            {
                return new ValidationResult(false, "请输入数字");
            }

            if ((age < Min) || (age > Max))
            {
                return new ValidationResult(false,
                  "数字大小必须在如下范围： " + Min + " - " + Max + ".");
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }
    }

}
